﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;

namespace YasharEl.Infrastructure.ResourcesManagement.Managers.Database
{
    public class SqlServerDataResourceReader : IDataResourceReader
    {
        public Hashtable PopulateResources(string connectionString, string baseName, string cultureName)
        {
            Hashtable resources = new Hashtable();
            string queryString = "select * from system_message_replacements where baseName=:BaseName and  culture=:Culture";

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = connection.CreateCommand();
                command.CommandText = queryString;
                command.Parameters.Add(":BaseName", DbType.AnsiString).Value = baseName;
                command.Parameters.Add(":Culture", DbType.AnsiString).Value = cultureName;
                try
                {
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        string name = reader["KEY"].ToString();
                        string value = reader["VALUE"].ToString();
                        resources.Add(name, value);
                    }
                    reader.Close();
                }
                catch (SqlException sqlEx)
                {
                    throw new InfrastructureCriticalException("Failed to populate database resource collection.", sqlEx);
                }
                catch (Exception ex)
                {
                    throw new InfrastructureCriticalException("Failed to populate database resource collection.", ex);
                }
            }
            return resources;
        }
    }
}
